package com.visitorspecials.tankbattle;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES10;
import android.opengl.GLSurfaceView.Renderer;

import java.io.*;
import java.util.*;

/*
 * Hi! Welcome to Ye-olde Graphics class! 
 * This class is managed by Dylan Reynolds, and should not be violated by anyone else
 * Trespassers will be shot
 * those still alive will be shot again
 * those found thinking of trespassing will also be shot.
 * 
 * Get off my damn lawn. Ya Yankee scum.
 */
public class OpenGLRenderer implements Renderer
{
	public ArrayList<Sprite> drawObjects;
	public boolean drawObjectsLock = true;
	public float width, height;

	public OpenGLRenderer()
	{
		drawObjects = new ArrayList<Sprite>();

		// if you're looking for the object setup, look in
		// TankBattleActivity.testSpriteParsing()
	}

	@Override
	public void onDrawFrame(GL10 gl)
	{
		try
		{
			while (drawObjectsLock)
				Thread.sleep(10);

			drawObjectsLock = true;

			gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

			for (Sprite s : this.drawObjects)
			{
				s.draw(gl);
			}

		}
		catch (Exception exc)
		{

		}

		drawObjectsLock = false;
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int w, int h)
	{
		this.width = w;
		this.height = h;

		gl.glViewport(0, 0, w, h);

		// make adjustments for screen ratio
		float ratio = (float) w / h;
		gl.glMatrixMode(GL10.GL_PROJECTION); // set matrix to projection mode
		gl.glLoadIdentity(); // reset the matrix to its default state
		gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7); // the projection matrix
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig arg1)
	{
		gl.glClearColor(1, 0.3f, 1, 1);
	}
}
